


import 'dart:async';

import 'package:bike_ride/api/common_repository.dart';
import 'package:bike_ride/common/mvvm/base_view_model.dart';
import 'package:bike_ride/common/util/account_manager.dart';
import 'package:bike_ride/common/widget/notifier_widget.dart';
import 'package:bike_ride/generated/l10n.dart';
import 'package:bike_ride/model/fault_report/model/server_parts_m.dart';
import 'package:bike_ride/model/fault_report/view/fault_report_v.dart';
import 'package:bike_ride/res/style/color_styles.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class FaultReportViewModel extends PageViewModel<FaultReportViewState> {
  CancelToken? cancelToken;
  ImageConfiguration devicePixelRatio = const ImageConfiguration(devicePixelRatio: 2);
  double mapZoom = 18;
  // 地图控制器
  final Completer<GoogleMapController> mapController = Completer<GoogleMapController>();
  Set<Marker> markers = {};
  Set<Polygon> areaPolygons = {};

  bool isRequestingVehicle = false;

  final address = ValueNotifier<String>('-');
  List<ServerPartsM> serverParts = [];

  @override
  onCreate() {
    cancelToken = CancelToken();
    pageDataModel = PageDataModel();
    getServerParts();
  }

  @override
  onDispose() {
    if (!(cancelToken?.isCancelled ?? true)) {
      cancelToken?.cancel();
    }
    super.onDispose();
  }

  Future getNearbyBike({required num lat, required num lng}) async {
    if (isRequestingVehicle) {
      return;
    }
    isRequestingVehicle = true;
    EasyLoading.show(status: '${S.current.loading}...');
    // 获取地址
    await getAddressByLgo(lat: lat, lng: lng);

    final result = await CommonRepository
        .getNearbyBikeIdl(cancelToken: cancelToken, lat: lat, lng: lng);
    EasyLoading.dismiss();
    final icon = await BitmapDescriptor.fromAssetImage(
        devicePixelRatio, 'assets/common_v2/ic_map_bike.png',
        mipmaps: false);
    final mMarkers = result
        .map((e) => Marker(
        markerId: MarkerId(e.vid!),
        icon: icon,
        position: LatLng(e.lat!.toDouble(), e.lng!.toDouble()),
        infoWindow: InfoWindow(title: e.vid!),
    ))
        .toList();
    markers = mMarkers.toSet();
    state.notifyChange();
    isRequestingVehicle = false;
  }

  Future getBrandAreaInfo() async {
    final result = await CommonRepository.getBrandAreaInfoIdl(cancelToken: cancelToken);
    if (result != null) {
      AccountManager.areaInfoM = result;
      final mAreaPolygon = Polygon(
          polygonId: PolygonId('${result.brandCode}_${result.areaCode}'),
          fillColor: ColorStyles.color_1E88E5.withOpacity(0.2),
          points: result.operateAreaPoints,
          strokeWidth: 3,
          strokeColor: ColorStyles.color_1E88E5.withOpacity(0.8));
      areaPolygons = {mAreaPolygon};
    }
    state.notifyChange();
  }


  Future getAddressByLgo({required num lat, required num lng}) async {
    final result = await CommonRepository.getAddressByLgoIdl(
      cancelToken: cancelToken,
      lat: lat,
      lng: lng,
    );
    address.value = result?.formattedAddress??'-';
  }

  Future getServerParts() async {
    final result = await CommonRepository.getServerPartsIdl(
      cancelToken: cancelToken,
    );
    serverParts = result;
    state.notifyChange();
  }

  Future<bool> faultReportSubmit({
    String? vid,
    String? place,
    String? orderId,
    num? lat,
    num? lng,
    List<String>? imgurl,
    String? content,
  }) async {
    return await CommonRepository.faultReportIdl(
      cancelToken: cancelToken,
      vid: vid,
      place: place,
      orderId: orderId,
      lat: lat,
      lng: lng,
      imgurl: imgurl,
      content: content,
    );

  }

  /// 请求数据
  @override
  Future<PageViewModel?> requestData({Map<String, dynamic>? params}) async {
    return null;

  }
}